package com.example.mysecondprojecct.test.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.mysecondprojecct.common.Result;
import com.example.mysecondprojecct.test.entity.Article;

import com.example.mysecondprojecct.test.mapper.ArticleMapper;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author MT
 * @since 2023-10-284
 */
@Slf4j
@RestController
@RequestMapping("/article")
public class ArticleController {
    @Resource
    ArticleMapper articleMapper;
    /**
     * Description: 增加文章
     * date: 2023/3/8
     * author: MT
     */
    @PostMapping("/add")
    public Result<?> addArticle(@RequestBody Article article, HttpServletRequest request){
        try {
//            LoginUser loginUser = (LoginUser) request.getAttribute("loginUser");
//            Integer uid = loginUser.getUserId();
            if( article.getTitle()==null || article.getCreateBy()== null || article.getUpdateBy() == null)
                return Result.faile("文章不合法");

            article.setArticleView(1);  //全部可见
            article.setArticleState(0); //审核中

            System.out.println(article);
            articleMapper.insert(article);
            return Result.success(null,"发布成功");

        }catch (Exception e){

            log.error(e + " error in  ArticleController->addArticle");
            return Result.faile(String.valueOf(e));
        }

    }

    /**
     * Description: 得到
     * date: 2023/3/8
     * author: MT
     */
    @GetMapping("/get")
    public Result<?> getArticle(){
        try {
            QueryWrapper<Article> wrapper = new QueryWrapper<>();
            wrapper.orderByDesc("id");
          List<Article> articles =  articleMapper.selectList(wrapper);
          return  Result.success(articles);
        }catch (Exception e){

            log.error(e + " error in  ArticleController->getArticle");
            return Result.faile(String.valueOf(e));
        }

    }
}

